import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(const DzhView());
}

class DzhView extends StatefulWidget {
  const DzhView() : super();

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new DzhViewState();
  }
}

class DzhViewState extends State<DzhView> with WidgetsBindingObserver {

  String nativeParams="";
  var channel = MethodChannel('dev.flutter.example/dzhview');
  @override
  void initState() {
    // TODO: implement initState
    // const channel = MethodChannel('dev.flutter.example/dzhview');
    channel.setMethodCallHandler((call) async{
      if(call.method == 'setParams'){
        setState(() {
          nativeParams = call.arguments as String;
        });
      }
    });
    super.initState();
  }


  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new MaterialApp(
      theme: new ThemeData(
        primaryColor: Colors.red,
      ),
      title: 'demo',
      home: new Scaffold(
        // appBar: new AppBar(
        //   title: new Text('Demo'),
        //   leading: Icon(Icons.menu,size: 30,),
        //   actions: <Widget>[
        //     Icon(Icons.search,size: 30,)
        //   ],
        // ),
        body: new Center(
          child: RaisedButton(
            // child: new Text("dzh"),
              child: new Text.rich(
                TextSpan(
                    text: 'notice:'+nativeParams,
                    style: TextStyle(fontSize: 11, color: Color(0xFF999999)),
                    children: [
                      TextSpan(
                        text: '《调用原生方法》',
                        style: TextStyle(color: Colors.blue, fontWeight: FontWeight.bold),
                        recognizer: TapGestureRecognizer()
                          ..onTap = () {
                            print('onTap');
                          },
                      ),
                    ]),
              ),
            onPressed: () async{
                var result=await channel.invokeListMethod('sendData',{'name': 'laomeng', 'age': 18});
                // var name = result['name'];
                // var age = result['age'];
                // setState(() {
                //   _data = '$name,$age';
                // });
            },

          ),
        ),
      ),
    );
  }

}
